﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class TruSoDA
    {
        public static TruSo GetByID(int maID)
        {
            TruSo loai = null;
            SqlConnection cn = DataProvider.Connect();
            //string str = "select @ten,@cm,@passpost,@ngaysinh,@diachi,@masothue,@giayphepkd,@nguoidaidien,@tenloai from khachhang,TruSo where maloai=TruSo,makh= " + @ma.ToString();
            SqlCommand cmd = new SqlCommand("Proc_TruSo_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTruSo", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new TruSo();
                loai.MaTruSo = maID;
                loai.Ten = Convert.ToString(dataReader["Ten"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                loai.SoDT = Convert.ToString(dataReader["SoDT"]);
                loai.Fax = Convert.ToString(dataReader["Fax"]);
                loai.MaLoaiTruSo = Convert.ToInt32(dataReader["LoaiTruSo"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.LoaiTruSo = LoaiTruSoDA.GetByID(loai.MaLoaiTruSo);
            }
            cn.Close();
            return loai;
        }

        public static List<TruSo> GetALl(int delFlg)
        {
            List<TruSo> dsTruSo = new List<TruSo>();
            TruSo loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TruSo_GetALL", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@delFlg", delFlg));

            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                loai = new TruSo();
                loai.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
                loai.Ten = Convert.ToString(dataReader["Ten"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                loai.SoDT = Convert.ToString(dataReader["SoDT"]);
                loai.Fax = Convert.ToString(dataReader["Fax"]);
                loai.MaLoaiTruSo = Convert.ToInt32(dataReader["LoaiTruSo"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                dsTruSo.Add(loai);
            }
            cn.Close();
            return dsTruSo;
        }

        public static int GetByTen(string ten)
        {
            TruSo loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TruSo_GetByTen", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Ten", ten));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new TruSo();
                loai.MaTruSo = Convert.ToInt32(dataReader["@maID"]);
                loai.Ten = ten;
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                loai.SoDT = Convert.ToString(dataReader["SoDT"]);
                loai.Fax = Convert.ToString(dataReader["Fax"]);
                loai.MaLoaiTruSo = Convert.ToInt32(dataReader["LoaiTruSo"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.LoaiTruSo = LoaiTruSoDA.GetByID(loai.MaLoaiTruSo);
            }
            cn.Close();
            return loai.MaTruSo;
        }

        //public static string GetByTen(int ma)
        //{
        //    TruSo loai = null;
        //    SqlConnection cn = DataProvider.Connect();
        //    SqlCommand cmd = new SqlCommand("Proc_TruSo_GetTen", cn);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@maTruSo", ma));
        //    SqlDataReader dataReader = cmd.ExecuteReader();
        //    if (dataReader.Read())
        //    {
        //        loai = new TruSo();
        //        loai.Ten = Convert.ToString(dataReader["Ten"]);
        //    }
        //    cn.Close();
        //    return loai.Ten;
        //}\

        public static List<TruSo> TruSoNew()
        {
            List<TruSo> l = new List<TruSo>();
            TruSo loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("sp_TruSo_New", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new TruSo();
                loai.MaLoaiTruSo = Convert.ToInt32(dataReader["MaLoaiTruSo"]);
                loai.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
                loai.Ten = Convert.ToString(dataReader["Ten"]);
                loai.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                loai.SoDT = Convert.ToString(dataReader["SoDT"]);
                loai.Fax = Convert.ToString(dataReader["Fax"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
            }
            cn.Close();
            return l;
        }

        public static List<TruSo> XemTruSo()
        {
            List<TruSo> l = new List<TruSo>();
            TruSo loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("sp_TruSo_All", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new TruSo();
                //loai.MaLoaiTruSo = Convert.ToInt32(dataReader["MaLoaiTruSo"]);
                loai.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
                loai.Ten = Convert.ToString(dataReader["Ten"]);
                loai.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                loai.SoDT = Convert.ToString(dataReader["SoDT"]);
                loai.Fax = Convert.ToString(dataReader["Fax"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
            }
            cn.Close();
            return l;

        }

    }
}
